package cn.tedu.account.service;

import cn.tedu.account.entity.Account;
import cn.tedu.account.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    @Transactional
    public void decrease(Long userId, BigDecimal money) {
        Account a = accountMapper.selectByUserId(userId);
        if (a.getResidue().compareTo(money) < 0) {
            throw new RuntimeException("可用金额不足.");
        }
        accountMapper.decrease(userId, money);
    }
}
